<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Base Helper class
 * @author cks
 */
class Lib_Base
{
	/**
	  * 危险字符转换
	  * @param  $string
	  * @return string
	  */
	public static function inputStr($string)
	{
	  	$string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/','',$string);
	  	$string = str_replace(array("\0","%00","\r"),'',$string);
	  	$string = str_replace(array("%3C",'<'),'<',$string);
	  	$string = str_replace(array("%3E",'>'),'>',$string);
	  	$string = str_replace(array('"',"'","\t",' ',"\\"),array('"',"'",' ',' '),$string);
	  	$string=trim($string);
	  	return htmlspecialchars($string, ENT_QUOTES);
	}
 
    /**
	  * 输出过滤，防止js注入等攻击
	  * @param  $string
	  * @return string
	  */
	public static function output_str_filter($string,$filter_level=0) {
		$string = preg_replace(array("/\"/"), array('&quot;'), $string);
		if($filter_level=1) {
             $string = preg_replace("@<script(.*?)>@is", "", $string); 
			//$string = preg_replace( "@<iframe(.*?)>@is", "", $string); 
			//$string = preg_replace( "@<style(.*?)>@is", "", $string); 
			//$string = preg_replace( "@<style(.*?)>@is", "", $string);
		}
		return htmlspecialchars($string);
          
	}

    /**
     * 时间转换输出
     * @param int $the_time
     * @return string
     */
    public static function time_tranx($show_time){
    	$now_time = time();
    	$dur = $now_time - $show_time;
    	if($dur < 0){
    		return $show_time;
    	}else{
    		
    		if($dur < 60){
    			return $dur.' second ago';
    		}elseif($dur < 3600){
    			return floor($dur/60).' minute ago';
    		}elseif($dur < 86400){
    			return floor($dur/3600).' hour ago';
    		}elseif ($dur < 86400*7){
    			return floor($dur/86400).' day ago';
    		}elseif($dur < 86400*30){
    			return floor($dur/(86400*7)).' week ago';
    		}elseif($dur < 86400*365){
    			return floor($dur/(86400*30)).' month ago';
    		}else{
    			return floor($dur/(86400*365)).' year ago';
    		}
    	}
    }
    /**
     * 过滤特殊字符及非英文数字
     * @param string $strParam
     * @return string
     */
    public static function replace_specialChar($strParam){
    	return preg_replace("/[^a-zA-Z0-9]+/","",$strParam);
    }
    /**
     * 数字转换输出
     * @param int number
     * @return string
     */
    public static function number_tranx($num){

      if(empty($num)||!is_numeric($num)){
          return 0;
      }
      if($num<999){
         return $num;
      }
      else if($num<999999){
        $p= stripos($num/1000, '.'); 
        return substr($num/1000,0,$p+2)."k";
      }
      else{
        $p= stripos($num/1000000, '.'); 
        return substr($num/1000000,0,$p+2)."m";
      }
    }
    public static function getWord($name,$cname){

        $u=Session::instance()->get('lag');

        if(empty($u)){

            $lag=Kohana::$config->load('en');
        }
        else{

            $lag=Kohana::$config->load($u);
        }

        if(empty($lag)||empty($lag[$name])){

            return $cname;
        }

        return $lag[$name];
    }
   /**
   *  获取分页
   */
  public static function page()
    {
      // Get page number
      $page = intval(Request::current()->query('page'));
      
      if(empty($page))
      {
        $page = intval(Request::current()->post('page'));
      }

      return ($page < 1) ? 1 : $page;
    }
    /**
   *  获取当前访问URL
   */
    public static function current_url($add_query = array(),$remove_param='')
    {     
      if(isset($_SERVER['HTTP_HOT_URI']))
    {
      $url_uri = $_SERVER['HTTP_HOT_URI'];
    }
    elseif(isset($_SERVER['REQUEST_URI']))
    {
      $url_uri = $_SERVER['REQUEST_URI'];
      
      if(stripos($url_uri, 'kohana_uri'))
      {
        $url_uri = str_replace('/index.php?kohana_uri=', '', $url_uri);

        $url_uri =  preg_replace('/&/i', '?', $url_uri,1);
      }
    }
    else
    {
      $url_uri='/';
    } 
    
    $url_uri = self::get_uri();
    
    $query_param = explode('?', $url_uri);    
    $param = array();
    
    if(count($query_param)>1)
    {
      $param_datas = explode('&', $query_param[1]);
      
      foreach ($param_datas as $data)
      {
        $data = explode('=', $data);
        
        $param[$data[0]] = $data[1];
      } 
    }
    
    if(!empty($remove_param) and isset($param[$remove_param]))
    {
      unset($param[$remove_param]);
    }
      
    $params = Arr::merge($param, $add_query);
  
    $query = http_build_query($params, '', '&');
    
    $query = urldecode($query);
    
    if($query ==='')
    {
      $url_uri = $query_param[0];
    }
    else 
    {
      $url_uri = $query_param[0].'?'.$query;
    } 
              
      return $url_uri;
    }
    
    public static function get_uri()
    {
      if(isset($_SERVER['HTTP_HOT_URI']))
      {
        $url_uri = $_SERVER['HTTP_HOT_URI'];
      }
      elseif(isset($_SERVER['REQUEST_URI']))
      {
        $url_uri = $_SERVER['REQUEST_URI'];
          
        if(stripos($url_uri, 'kohana_uri'))
        {
          $url_uri = str_replace('/index.php?kohana_uri=', '', $url_uri);
      
          $url_uri =  preg_replace('/&/i', '?', $url_uri,1);
        }
      }
      else
      {
        $url_uri='/';
      }
      
      return $url_uri;
    }

    public static function is_mobile(){

        $detect = new Lib_Detect();  

        if ($detect->isMobile()) {
            return true;
        }

        return false;
    }

    ////获得访客浏览器语言
    public static function lag(){
        if(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
            $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
            $lang = substr($lang,0,5);
            if(preg_match("/zh-cn/i",$lang)){
                $lang = "cn";
            }
            elseif(preg_match("/zh/i",$lang)){
                $lang = "cn";
            }
            else{
              $lang = "en";
            }
            return $lang;
        }
        else{
            return "en";
        }
  }
}
